package com.boarsoft.boar.demo.user.service;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import com.boarsoft.boar.demo.user.biz.PocAccountBiz;
import com.boarsoft.boar.demo.user.entity.PocAccount;
import com.boarsoft.dao.sharding.bean.DaoSharding;
import com.boarsoft.demo.gateway.server.entity.PocAccountReverseRequest;
import com.boarsoft.demo.gateway.server.entity.PocAccountTransferRequest;
import com.boarsoft.demo.gateway.server.service.PocAccountTransferService;

@Component("accountService")
public class PocAccountServiceImpl implements PocAccountService {
	
	private static final Logger log = LoggerFactory.getLogger(PocAccountServiceImpl.class);

	private static final String dateFormate = "yyyy-MM-dd";
	
	@Autowired
	private PocAccountBiz accountBiz;
	
	@Lazy
	@Autowired
	private PocAccountTransferService accountTransferServ;

	@Override
	@DaoSharding(policy = "tenant", index = 0)
	public boolean add(int tenantId, PocAccount p) {
		return accountBiz.add(tenantId, p);
		
	}

	@Override
	@DaoSharding(policy = "tenant", index = 0)
	public boolean update(int tenantId, PocAccount p) {
		return accountBiz.update(tenantId, p);
		
	}
	
	@Override
	@DaoSharding(policy = "tenant", index = 0)
	public PocAccount find(int tenantId, int userId) {
		return accountBiz.findByUser(tenantId, userId);
	}

	@Override
	public boolean accountTransfer(String fromAccount, String toAccount, BigDecimal amount) {

		log.info("开始调用gateway的transfer");
		
		PocAccountTransferRequest request = new PocAccountTransferRequest();
		request.setI1CRACNO(fromAccount);
		request.setI1CRACNM("南昌农村商业银行股份有限公司");
		request.setI1DBACNO(toAccount);
		request.setI1DLEDDT(new SimpleDateFormat(dateFormate).format(new Date()));
		request.setI1BKAMOT(amount);
		request.setI1DBVCTP(110);
		
		System.out.println("转账金额：" + NumberFormat.getCurrencyInstance().format(amount));
		
		boolean flag = accountTransferServ.transfer(request);
		
		return flag;
		
	}

	@Override
	public boolean accountReverse(String logId, String logDate) {
		
		log.info("开始调用gateway的reverse");
		
		PocAccountReverseRequest reverse = new PocAccountReverseRequest();
		reverse.setI1RSCNSQ(logId);
		reverse.setI1RSWKDT(logDate);
		reverse.setI1S1REMK("258025122415485602008300006400");
		reverse.setI1S2REMK("25802412241548280763");
		reverse.setI1RSCNCD(54);
		
		boolean flag = accountTransferServ.reverse(reverse);
		
		return flag;
		
	}

}
